/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.guobo.web;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.guobo.entity.WarningRole;
import com.thinkgem.jeesite.modules.guobo.entity.WarnroleRfid;
import com.thinkgem.jeesite.modules.guobo.service.WarningRoleService;
import com.thinkgem.jeesite.modules.guobo.service.WarnroleRfidService;

/**
 * 预警规则Controller
 * @author wudavid
 * @version 2017-03-09
 */
@Controller
@RequestMapping(value = "${adminPath}/guobo/warningRole")
public class WarningRoleController extends BaseController {

	@Autowired
	private WarningRoleService warningRoleService;
	
	@Autowired
	private WarnroleRfidService warnroleRfidService;
	@ModelAttribute
	public WarningRole get(@RequestParam(required=false) String id) {
		WarningRole entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = warningRoleService.get(id);
		}
		if (entity == null){
			entity = new WarningRole();
		}
		return entity;
	}
	
	@RequiresPermissions("guobo:warningRole:view")
	@RequestMapping(value = {"list", ""})
	public String list(WarningRole warningRole, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<WarningRole> page = warningRoleService.findPage(new Page<WarningRole>(request, response), warningRole); 
		model.addAttribute("page", page);
		return "modules/guobo/warningRoleList";
	}

	@RequiresPermissions("guobo:warningRole:view")
	@RequestMapping(value = "form")
	public String form(WarningRole warningRole, Model model) {
		model.addAttribute("warningRole", warningRole);
		if(StringUtils.isNotBlank(warningRole.getId())){
			List<WarnroleRfid> roleRfids = warnroleRfidService.findListByWarnroleId(warningRole.getId(),null);
			model.addAttribute("roleRfids", roleRfids);
		}
		return "modules/guobo/warningRoleForm";
	}

	@RequiresPermissions("guobo:warningRole:edit")
	@RequestMapping(value = "save")
	public String save(WarningRole warningRole, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, warningRole)){
			return form(warningRole, model);
		}
		warningRoleService.save(warningRole);
		addMessage(redirectAttributes, "保存预警规则成功");
		return "redirect:"+Global.getAdminPath()+"/guobo/warningRole/?repage";
	}
	
	@RequiresPermissions("guobo:warningRole:edit")
	@RequestMapping(value = "delete")
	public String delete(WarningRole warningRole, RedirectAttributes redirectAttributes) {
		warningRoleService.delete(warningRole);
		addMessage(redirectAttributes, "删除预警规则成功");
		return "redirect:"+Global.getAdminPath()+"/guobo/warningRole/?repage";
	}

	/**
	 * ajax方式保存
	 * @param warningRole
	 * @param response
	 * @return
	 */
	@RequiresPermissions("guobo:warningRole:edit")
	@ResponseBody
	@RequestMapping(value = "saveWarningRole")
	public Map<String, Object> saveWarningRole(WarningRole warningRole,Model model,
			HttpServletRequest request,HttpServletResponse response) {
		Map<String, Object> data = new HashMap<String, Object>();
		String warningRoleId = warningRoleService.saveWarningRole(warningRole);
		data.put("code", "SUCCESS");
		data.put("id", warningRoleId);
		return data;
	}
	
	@RequiresPermissions("guobo:warningRole:view")
	@RequestMapping(value = "findListByWarnroleId")
	public String findListByWarnroleId(WarningRole warningRole, HttpServletRequest request,HttpServletResponse response,Model model) {
		String rfid = request.getParameter("rfid");
		Map<String,String> param = new HashMap<String,String>();
		param.put("rfid", rfid);
		model.addAttribute("warningRole", warningRole);
		if(StringUtils.isNotBlank(warningRole.getId())){
			List<WarnroleRfid> roleRfids = warnroleRfidService.findListByWarnroleId(warningRole.getId(),param);
			model.addAttribute("roleRfids", roleRfids);
		}
		return "modules/guobo/roleRfidList";
	}

}